package com.cti.ftpservice.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class FileHelper {
	
	static String basePath;
	static String WEB_INF_PATH;
	static{
		String path=FileHelper.class.getClassLoader().getResource("").getPath();
		File file=new File(path);
		basePath= file.getParentFile().getParent();
		WEB_INF_PATH=file.getParent();
	}
	
	private static Log logger = LogFactory.getLog(FileHelper.class);
	/**
	 * 获取classpath基路径
	 * @return
	 */
	public static String getBaseClassPath(){
		return basePath;
	}
	
	public static String getWebInfoPath(){
		return WEB_INF_PATH;
	}
	
	public static String createDirWithBasePath(String basePath,String fullFilenameToAdd){
		String dirPath=FilenameUtils.concat(basePath, fullFilenameToAdd);
		File file=new File(dirPath);
		if(!file.exists()){
			file.mkdirs();
		}
		return file.getAbsolutePath();
	}
	
	/**
	 * 获取基于basepath的文件路径
	 * @param fullFilenameToAdd
	 * @return
	 */
	public static String getFullFilePathWithBasePath(String fullFilenameToAdd){
		return FilenameUtils.concat(basePath, fullFilenameToAdd);
	}
	
	/**
	 * 获取基于basepath的文件路径
	 * @param fullFilenameToAdd
	 * @return
	 */
	public static String getFullFilePathWithBasePath(String dirPath,String filePath){
		String tempDirPath = FilenameUtils.concat(basePath, dirPath);
		return FilenameUtils.concat(tempDirPath, filePath);
	}
	
	/**
	 * 判断并创建文件夹，该文件夹相对于classpath基路径
	 * @param dirPath
	 * @return
	 */
	public static String createDirWithBaseClassPath(String fullFilenameToAdd){
		String basePath=getBaseClassPath();
		return createDirWithBasePath(basePath, fullFilenameToAdd);
	}
	
	public static byte[] readBytesFromInputstream(InputStream in) throws IOException {  
        BufferedInputStream bufin = new BufferedInputStream(in);  
        int buffSize = 1024;  
        ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);  
        byte[] temp = new byte[buffSize];  
        int size = 0;  
        while ((size = bufin.read(temp)) != -1) {  
            out.write(temp, 0, size);  
        }  
        bufin.close();  
        byte[] content = out.toByteArray();  
        out.close();
        in.close();
        return content;  
    }
	
	public static String readStringFromInputstream(InputStream in,String charset) throws IOException {  
        byte[] bs=readBytesFromInputstream(in);
        return new String(bs,charset);
    }
	
	/**
	 * 获取文件大小
	 * @param fileSize
	 * @return
	 */
	public static String getFileSizeStringFromInt(long fileSize){
		DecimalFormat df = new DecimalFormat("#.00");
		String fileSizeString = "";
		String wrongSize="0B";
		if(fileSize==0){
		return wrongSize;
		}
		if (fileSize < 1024){
		fileSizeString = df.format((double) fileSize) + "B";
		 }
		else if (fileSize < 1048576){
		fileSizeString = df.format((double) fileSize / 1024) + "KB";
		}
		else if (fileSize < 1073741824){
		    fileSizeString = df.format((double) fileSize / 1048576) + "MB";
		  }
		else{
		    fileSizeString = df.format((double) fileSize / 1073741824) + "GB";
		  }
		return fileSizeString; 
	}
	
	
	public static void moveFileToNewPath(String orignalFileFullPath,String newPath)  {
		try {
			File oldFile=new File(orignalFileFullPath);
			if(!oldFile.exists())
				return;
			File newFileDirectory = new File(newPath);
			if(!newFileDirectory.exists())
				newFileDirectory.mkdirs();
			String newFileFullPath=newPath+oldFile.getName();
			oldFile.renameTo(new File(newFileFullPath));
		} catch (Exception e) {
			logger.error(e.getCause().toString());
		}
		
	}
}
